﻿$(function() {
    /**
     * 电度
     * */
    var daybar = echarts.init(document.getElementById('daybar'));
    optionBar = {
	    tooltip : {
	        trigger: 'axis',
	        axisPointer : {            
	            type : 'shadow'        
	        }
	    },
	    grid: {
            top: '12%',
            left: '5%',
            right: '6%',
            bottom:'20%',
            containLabel: true
        },
	    legend: {
	    	bottom: '25',
	        data: ['有功电度']
	    },
	    backgroundColor:'#e0e0e0',
	    xAxis:  {
        	type: 'category',
        	nameGap:5,
        	data: [],
//            axisLabel:{
//	    		formatter: '{value}周'
//	    	 },
        	splitLine:{
            	show:false
            }
	    },
	    yAxis: {
	    	 type: 'value',
	    	 name:'有功电度(度)',
	    	 axisLabel:{
	    		 formatter: '{value}'
	    	 },
	    	 splitLine:{
	            	show:false
	            }
	    },
	    dataZoom:{
	    	type:'slider',
	    	start:0,
	    	end:100,
	    	bottom:1
	    },
	    series: [{
            name: '有功电度',
            type: 'bar',
            barMaxWidth:30,
            data:[]
        }]
	};
    daybar.setOption(optionBar);
    
	/**
	 * 电度电费
	 * */
	var daypie = echarts.init(document.getElementById('daypie'));
	optionpie = {
		    tooltip: {
		        trigger: 'item',
		        formatter: "{a} <br/>{b}: {c} ({d}%)"
		    },
		    backgroundColor:'#e0e0e0',
		    legend: {
		        orient: 'vertical',
		        x: 'right',
		        data:[]
		    },
		    series: [
		        {
		            name:'电费',
		            type:'pie',
		            radius: [0, '40%'],

		            label: {
		                normal: {
		                    position: 'inside'
		                }
		            },
		            labelLine: {
		                normal: {
		                    show: false
		                }
		            },
		            data:[]
		        },
		        {
		            name:'电度',
		            type:'pie',
		            radius: ['55%', '80%'],

		            data:[]
		        }
		    ]
		};
	daypie.setOption(optionpie);
	
	/**
	 * 设备开动情况
	 * */
    var dayline = echarts.init(document.getElementById('dayline'));
    optionline = {
        tooltip : {
            trigger: 'axis',
            axisPointer: {
		        type: 'shadow'
		    }
        },
        backgroundColor:'#e0e0e0',
        grid: {
            top: '15%',
            left: '4%',
            right: '5%',
            bottom:'20%',
            containLabel: true
        },
        legend: {
        	bottom: '25',
	        data:['平均开动率','平均满载率']
	    },
        xAxis:  {
            type: 'category',
            boundaryGap: [1, 1],
            data: [],
//            axisLabel:{
//	    		formatter: '{value}周'
//	    	 },
            splitLine:{
            	show:false
            }
        },
        yAxis: {
        	min:0,
        	max:100,
            type: 'value',
            name:'开动率(%)',
            axisLabel:{
	    		 formatter: '{value}'
	    	 },
            splitLine:{
            	show:false
            }
        },
        dataZoom:{
	    	type:'slider',
	    	start:0,
	    	end:100,
//	    	labelFormatter: function (value) {
//	    	    return value; // 返回最终的label内容。
//	    	},
	    	bottom:1
	    },
        series: [{
             name:'平均开动率',
             type:'line',
             data:[]
         },{
             name:'平均满载率',
             type:'line',
             data:[]
         }]
    };
    dayline.setOption(optionline);
     
    var lineId=0;var equId=0;
    var	startWeek="";
    var	endWeek="";
    var year=moment().year();
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startWeek=value[0];
	       	endWeek=value[1];
	       	getData();
       }
    });
    
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 
    var vm2 = new Vue({
	    el: '#itemData2',
	    data:{item:[]}
	}); 
    var currentWeek=moment().week();
    var weeks="";
    if(currentWeek<8){
    	weeks=[1,currentWeek];
    }else{
    	weeks=[moment().subtract(7, 'w').week(),currentWeek];
    }
    $('#ss').slider('setValues',weeks);
    var arr=$('#ss').slider('getValues');
    startWeek=arr[0];
    endWeek=arr[1];
    getData();
    function getData(){
    	$.post(basePath+"/line/lineComprehensiveWeek.json",{"lineId":lineId,"equId":equId,year:year,startTime:startWeek,endTime:endWeek},function(res){
    		vm.$set('item',res);
    		vm2.$set('item',res);
//    		if(res.success){
    		daypie.setOption({
				legend: {
			        data:res.legType
			    },
			    series:[{
			    	name:'电费',
			    	data:res.chargePie
			    },{
			    	name:'电度',
			    	data:res.electricityPie
			    }]
			});
			dayline.setOption({
		        xAxis: {
		            data: res.startRateWeekss
		        },
		        series: [{
		            name:'平均开动率',
		            data:res.startRate
		        },{
		            name:'平均满载率',
		            data:res.fullRate
		        }]
			});
			daybar.setOption({
				xAxis: {
		            data: res.weeks
		        },
		        series: [{
		            data: res.weekElecricity
		        }]
			});
//    		}else{
//    			alert("无数据，请重新选择时间区间");
//    		}
    	});
    }
    
  //点击事件
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	//定义时间的表单
	$('#defineTimeWin').dialog({
		title:'自定义时间',
		width:400,
		height:150,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'确定',
			iconCls:'icon-add-new',
			handler:function(){
					var st=$("input[name='startTime']").val();
					var cmax=moment(st, "YYYY").weeksInYear();
					year=st;
					$('#ss').slider({
						max:cmax
					});
					getData();
					$('#defineTimeWin').dialog('close');

			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#defineTimeWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#defineTimeform').form('reset');
		}
	});
	 /**
	  * 切换设备的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
				if(node.shifts){
					lineId=node.id;
		 			var arr=$('#ss').slider('getValues');
		 			startWeek=arr[0];
		 			endWeek=arr[1];
		 			getData();
		 			mePanel.dialog('close');

				}else{
					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
				}
				lineId=node.id;
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
	$("#switchEquipOK").click(function(){
		var row = $("#linetree").tree('getSelected');
		if(row&&row.url!=null){
			lineId=row.id;
			var arr=$('#ss').slider('getValues');
			startWeek=arr[0];
			endWeek=arr[1];
			getData();
			mePanel.dialog('close');
		}else{
			$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
		}
	});


/**
 * 选择设备
 */
	$("#switchEquipbtn").click(function(){
		var equPanel = $('#allEquipsByLine');
		equPanel.dialog('open');
		$('#equip_data').datagrid({    
		    url: basePath+"/equip/findByLine.json",
		    queryParams:{lineId:lineId}
		});
	});
	$('#equip_data').datagrid({
		//双击设备时，根据选中设备刷新
		onDblClickRow: function(rowIndex, rowData){
			equId=rowData.id;
		    getData();
		   $("#allEquipsByLine").dialog('close');
		}
	});
	$("#switchEquipByLineOK").click(function(){
	   	var equWin=$("#allEquipsByLine");
	   	var row=$('#equip_data').datagrid('getSelected');
	   	equId=row.id;
	   	getData();
	   	equWin.dialog('close');
	});


});